package com.zlfund.cat.zjgsmk.widget;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapShader;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.RectF;
import android.graphics.Shader;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;

import com.bumptech.glide.disklrucache.DiskLruCache;
import com.bumptech.glide.load.engine.cache.DiskLruCacheFactory;
import com.zlfund.cat.zjgsmk.ui.base.ZApplication;
import com.zlfund.cat.zjgsmk.util.BitmapHelper;
import com.zlfund.cat.zjgsmk.util.DensityUtils;
import com.zlfund.cat.zjgsmk.util.Logger;
import com.zlfund.cat.zjgsmk.util.VersionUtil;

import java.io.File;
import java.io.IOException;

/**
 * Created by chenj on 2017/9/4 0004.
 */

public class HeadView extends View {

    private int mHeight;
    private int mWidth;
    private Bitmap mBlurBm;
    private Paint mP;
    private RectF mRectf;
    private Path mPath;
    private final int BOTTOMHEIGHT;
    private final float HEIGHT_PARAM = 0.6f;
    private DiskLruCache mLruCache;
    private Bitmap mBitmap;

    public HeadView(Context context) {
        this(context, null);
    }

    public HeadView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public HeadView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mP = new Paint();
        mP.setAntiAlias(true);
        mPath = new Path();
        BOTTOMHEIGHT = DensityUtils.dp2px(context,10);
        try {
            mLruCache = DiskLruCache.open(new File(DiskLruCacheFactory.DEFAULT_DISK_CACHE_DIR), VersionUtil.getVersionCode(context),20,30L * 1024L *1024L);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        mWidth = ZApplication.SCREEN_WIDTH;
        mHeight = (int) (mWidth * HEIGHT_PARAM);
        setMeasuredDimension(ZApplication.SCREEN_WIDTH, mHeight + BOTTOMHEIGHT);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        Logger.i("onDraw");
        if (mBlurBm != null) {
            if (mRectf == null) {
                mRectf = new RectF(-mWidth * 0.5f, mHeight - 2 * mWidth, mWidth * 1.5f, mHeight);
            }
            mPath.reset();
            if (mBitmap == null) {
                mBitmap = BitmapHelper.blur(getContext(), BitmapHelper.scaleBm(mBlurBm, mWidth, mHeight), this);
            }
            BitmapShader shader = new BitmapShader(mBitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
            mP.setShader(shader);
            mPath.moveTo(0, 0);
            mPath.lineTo(0, (float) Math.sqrt(5));
            mPath.arcTo(mRectf, 120, -60, false);
            mPath.lineTo(mWidth, 0);
            mPath.close();
            canvas.drawPath(mPath, mP);
        }
    }

    public void setBlurBm(Bitmap blurBm) {
        mBlurBm = blurBm;
        mBitmap = null;
        invalidate();
    }
}
